package com.afd.ozblip.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.afd.ozblip.adapter.IntegralListAdapter;
import com.afd.ozblip.adapter.UserCouponListAdapter;
import com.afd.ozblip.entity.UserIntegralEntity;
import com.afd.ozblip.entity.UserIntegralResult;
import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.ParseJson;
import com.afd.ozblip.util.StringUtil;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class UserIntegralActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private TextView levelTv;
	private TextView totalIntegralTv;
	private ListView integralListView;
	/****************** 其它变量 **************************/
	private List<UserIntegralEntity> integralList;
	private UserIntegralResult integralResult;
	private IntegralListAdapter integralListAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_integral_detail);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
		// 获取数据
		requestUserIntegralData();
	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.integralListView = (ListView) findViewById(R.id.lv_integral_list);
		this.levelTv = (TextView) findViewById(R.id.tv_integral_level);
		this.totalIntegralTv = (TextView) findViewById(R.id.tv_integral_total);
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {
		
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		Intent intent = getIntent();
	}

	/**
	 * 获取用户积分明细数据
	 */
	private void requestUserIntegralData() {
		if (commonUserInfo == null) {
			return;
		}
		AsyncLoadData ald = new AsyncLoadData(this, "加载中...",
				new AsyncAction() {

					@Override
					public void doLoad() throws Exception {
						// 获取提交参数
						Map<String, Object> addReviewParamMap = new HashMap<String, Object>();
						addReviewParamMap.put("userId",
								commonUserInfo.getUserId());
						addReviewParamMap.put("language", commonLanguage);

						// 发送请求
						byte[] bUserDetail = HttpUtil.requestByPost(
								UserIntegralActivity.this,
								Constants.USER_INTEGRAL_API, addReviewParamMap);
						if (bUserDetail != null) {
							integralResult = new ParseJson()
									.parseUserIntegralListJson(bUserDetail);
							integralList = integralResult.getUserPointMin();
						}
					}

					@Override
					public void loadEnd() throws Exception {
						setViews();
						setUserCouponListAdapter();
					}

				});
		ald.execute(0);

	}

	/**
	 * 调用优惠券列表适配器
	 */
	private void setUserCouponListAdapter() {
		if (CollectionUtil.UnNullAndZero(integralList)) {
			this.integralListAdapter = new IntegralListAdapter(
					UserIntegralActivity.this);
			this.integralListAdapter.addItems(integralList);
			this.integralListView.setAdapter(integralListAdapter);

		}
	}
	private void setViews(){
		if(integralResult!=null){
			String userLevel = integralResult.getUserLevel();
			if(StringUtil.isNullOrEmpty(userLevel)){
				this.levelTv.setText("LV1");
			}else{
				this.levelTv.setText("LV"+userLevel);
			}
			String userBadge = integralResult.getUserBadge();
			String userPoint = integralResult.getUserPoint();
			if(StringUtil.isNullOrEmpty(userPoint)){
				this.totalIntegralTv.setText("0");
			}else{
				this.totalIntegralTv.setText(userPoint);
			}
		}
	}
}